package UDP.moreANDmore;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/*
 * UDP客户端 循环发送数据
 * */
public class UDPClientDemo1 {
    public static void main(String[] args) {
        System.out.println("=====客户端启动=====");

        try {
            //1、创建UDP发送端的socket对象
            DatagramSocket socket = new DatagramSocket();
            //键盘录入数据
            Scanner sc = new Scanner(System.in);
            while (true) {
                //2、输入数据并打包
                System.out.println("请输入数据:");
                String msg = sc.next();

                //用字符去匹配，不会出现错误，如果反过来比较，可能会出错
                if ("886".equals(msg)) {
                    System.out.println("=====客户端退出=====");
                    break;
                }
                byte[] data = msg.getBytes();
                DatagramPacket packet = new DatagramPacket(data, data.length,
                        InetAddress.getLocalHost(), 8080);
                //3、发送数据
                socket.send(packet);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
